<?php


class bd_utils{
	var $serveur;
	var $user;
	var $password;
	var $database;
	var $connect;
	var $bigDist;
	var $smallDist;


	function bd_utils($database="runner",$user="root",$password="",$serveur="127.0.0.1"){
		$this->database=$database;
		$this->user=$user;
		$this->password=$password;
		$this->serveur=$serveur;

	}

	function setDistances($smallDist,$bigDist){
		$this->selectDB("runner");
		$queryresult=mysql_query("update system set smallDistance=$smallDist,bigDistance=$bigDist where hash='SYSTEM_DATA'");
		 
	}
	function getComa($data){
		return "'".$data."'";
	}
	function updateUser($currentIds,$currentNom,$currentPrenom,$currentDossard,$currentDistance,$currentClub){
		$this->selectDB("runner");
			
		$currentNom=$this->getComa($currentNom);
		$currentPrenom=$this->getComa($currentPrenom);
		$currentDossard=$this->getComa($currentDossard);
		$currentDistance=$this->getComa($currentDistance);
		$currentClub=$this->getComa($currentClub);
		$query="update runner set nom=".$currentNom.",prenom=".$currentPrenom.",dossard=".$currentDossard.",distance=".$currentDistance.",club=".$currentClub." where id=".$currentIds."";
		$queryresult=mysql_query($query);
	
	}
	function getSmalDistance(){
		$this->selectDB("runner");
		$queryresult=mysql_query("select smallDistance from system where hash='SYSTEM_DATA'");
		$row=mysql_fetch_assoc($queryresult);
		return $row['smallDistance'];
	}
	function getBigDistance(){
		$this->selectDB("runner");
		$queryresult=mysql_query("select bigDistance from system where hash='SYSTEM_DATA'");
		$row=mysql_fetch_assoc($queryresult);
		return $row['bigDistance'];
	}

	function connect(){

		if ($this->connect =mysql_connect("localhost","root","")){
			return true;
		}

		return false;


	}


	function close(){

		if (isset($this->connect)){
			if (mysql_close($this->connect) ){

				return true;
			}
			return false;

		}else{

			echo "la connexion bd ne peut ètre fermée car elle n\'a pas été préalablement ouverte";

		}

	}

	function selectDB($db){
		mysql_select_db($db,$this->connect);
	}
	function manageNewRunnerData($nom,$prenom,$date,$sexe,$club){
		$this->selectDB("runner");
		$query = sprintf("select * from runnerData where upper(nom)=upper('%s') AND upper(prenom)=upper('%s') and date='%s-00-00' and sexe='%s'",
		mysql_real_escape_string($nom),
		mysql_real_escape_string($prenom),
		mysql_real_escape_string($date),
		mysql_real_escape_string($sexe));

		$queryresult=mysql_query($query);
		if (!$queryresult) {
			$message  = 'Requete invalide : ' . mysql_error() . "\n";
			$message .= 'Requete complete : ' . $query;
			die($message);
		}
		if(mysql_num_rows($queryresult)==0){
			$queryresult=mysql_query("insert into runnerData (nom,prenom,date,sexe,club)values('".$nom."','".$prenom."','".$date."-00-00','".$sexe."','".$club."')");
		}

		return $queryresult;


	}
	function saveRunner($nom,$prenom,$date,$sexe,$dossard,$club,$distance){
		$this->selectDB("runner");
		$this->manageNewRunnerData($nom,$prenom,$date,$sexe,$club);
		$queryresult=mysql_query("insert into runner (nom,prenom,date,sexe,dossard,club,distance)values('".$nom."','".$prenom."','".$date."','".$sexe."','".$dossard."','".$club."','".$distance."')");
		if (!$queryresult) {
			$message  = 'Requete invalide : ' . mysql_error() . "\n";
			$message .= 'Requete complete : ' . $query;
			die($message);
		}
		return $queryresult;
	}


	function getAllRunners(){
		$this->selectDB("runner");
		$queryresult=mysql_query("select * from runner where dossard!=0");


		$i=0;
		while ($row= mysql_fetch_assoc($queryresult)){

			$tab[$i]=$row;
			$i++;

		}

		if(isset($tab)){
			return $tab;
		}
		return 0;



	}

	function executeQuery($query){
		$this->selectDB("runner");

		$queryresult=mysql_query($query);
		$i=0;
		if(!$queryresult){
			$message  = 'Requete invalide : ' . mysql_error() . "\n";
			$message .= 'Requete complete : ' . $query;
			die($message);
		
		}
		while ($row= mysql_fetch_assoc($queryresult)){

			$tab[$i]=$row;
			$i++;

		}

		if(isset($tab)){
			return $tab;
		}
		return 0;

	}

	function setRunner($dossard){
		$this->selectDB("runner");
		$queryresult=mysql_query("select * from runner where dossard='".$dossard."'");
		if(!$queryresult){
			$message  = 'Requete invalide : ' . mysql_error() . "\n";
			$message .= 'Requete complete : ' . $query;
			die($message);
		
		}
		$row=mysql_fetch_assoc($queryresult);
		$id=$row['id'];
		$nom=$row['nom'];
		$prenom=$row['prenom'];

		$queryresult=mysql_query("insert into resultats (FK_runner,nom,prenom) values (".$id.",'".$nom."','".$prenom."')");
		if(!$queryresult){
			$message  = 'Requete invalide : ' . mysql_error() . "\n";
			$message .= 'Requete complete : ' . $query;
			die($message);
		
		}
		return $dossard;


	}
	function resetDB(){
		$this->selectDB("runner");
		$queryresult=mysql_query("delete from runner");
		$queryresult=mysql_query("delete from resultats");
		return $queryresult;

	}

	function setTime(){

		$this->selectDB("runner");
		$queryresult=mysql_query("delete from timer_setting where time='time'");
		$queryresult=mysql_query("insert into timer_setting (time) values ('time')");
		return $queryresult;

	}

	function getTime(){

		$this->selectDB("runner");

		$queryresult=mysql_query("select * from timer_setting");
		$row=mysql_fetch_assoc($queryresult);
		$time=$row['time_reference'];
		return $time;

	}

	function getleftRunners(){
		$tableDone=$this->executeQuery("select * from resultats");
		$tableAll=$this->executeQuery("select * from runner where dossard !=0");
		$tableLeft[0]=0;
		$k=0;
		$i=0;

		while($i<sizeof($tableAll)){
				
			$found=false;
			$j=0;
				
			while($j<sizeof($tableDone) && !$found){
				$idDone=$tableDone[$j]['FK_runner'];
				$idAll=$tableAll[$i]['id'];
				$found=$idDone==$idAll;

				$j++;
			}
				
			if(!$found){
				$tableLeft[$k]=$tableAll[$i];
				$k++;
			}
			$i++;
		}




		return $tableLeft;
	}
}
?>